package app;

import database.JDBCUtils_Druid;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Scanner;


public class views {
    public static void main(String[] args) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils_Druid.getDataSource());

        while (true) {
            Scanner scan = new Scanner(System.in);
            System.out.println("个人通讯录系统-B/S结构：");
            System.out.println("1.查看 2.新增 3.删除 4.修改 5.退出");
            System.out.println("请输入选项:");
            int choice = scan.nextInt();
            if (choice == 1) {
                List<AddressList> all = Utils.findAll(jdbcTemplate);
                for (AddressList item : all) {
                    System.out.println(item);
                }
            } else if (choice == 2) {
                System.out.println("请输入id：");
                Integer id = scan.nextInt();
                System.out.println("请输入姓名：");
                String name = scan.next();
                System.out.println("请输入地址：");
                String address = scan.next();
                System.out.println("请输入号码：");
                String phone = scan.next();
                Utils.add(new AddressList(id, name, address, phone), jdbcTemplate);
                System.out.println("修改新增成功");
            } else if (choice == 3) {
                System.out.println("请输入删除对象id：");
                Integer id = scan.nextInt();
                Utils.delete(id, jdbcTemplate);
                System.out.println("删除成功");
            } else if (choice == 4) {
                System.out.println("请输入修改对象id：");
                Integer id = scan.nextInt();
                System.out.println("请输入姓名：");
                String name = scan.next();
                System.out.println("请输入地址：");
                String address = scan.next();
                System.out.println("请输入号码：");
                String phone = scan.next();
                Utils.update(new AddressList(id, name, address, phone), jdbcTemplate);
                System.out.println("修改成功");
            } else {
                System.exit(0);
            }
        }
    }
}
